package com.woniuxy.microgoodsserver.sortmag.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microcommonserver.bean.gy.GoodCategoryInfo1;
import com.woniuxy.microcommonserver.bean.gy.GoodCategoryInfo2;
import com.woniuxy.microcommonserver.pageHelper.PageRequest;
import com.woniuxy.microgoodsserver.sortmag.mapper.GoodCategoryMapper;
import com.woniuxy.microgoodsserver.sortmag.service.GoodCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**分类业务实现类
 * @author ：苟杨
 * @description：TODO
 * @date ：2021/1/22 9:45
 */
@Transactional(readOnly=true)
@Service
public class GoodCategoryServiceImpl implements GoodCategoryService {

    @Resource
    private GoodCategoryMapper goodCategoryMapper;
    /**
        分类添加
     * @params:
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void addSort(GoodCategoryInfo1 goodCategoryInfo1) {

        //判断上级是哪个目录
        if(goodCategoryInfo1.getUpperName().equals("所有分类")){
            goodCategoryMapper.addSort1(goodCategoryInfo1);
        }else {
            //根据UpperName查询父类ID
            Integer parentId = goodCategoryMapper.findIdByParentName(goodCategoryInfo1.getUpperName());
            //添加到goodCategoryInfo2表中
            goodCategoryMapper.addSort2(goodCategoryInfo1,parentId);
        }

    }
    /**
        查询目录是否存在
     * @params:
     * @return
     */
    @Override
    public GoodCategoryInfo1 findByName(GoodCategoryInfo1 goodCategoryInfo1) {
        //判断上级是哪个目录
        if(goodCategoryInfo1.getUpperName().equals("所有分类")){
            GoodCategoryInfo1 parentList = goodCategoryMapper.findParentList(goodCategoryInfo1);
            return parentList;
        }else {
            //查询子类目录是否存在
            GoodCategoryInfo1 sonList = goodCategoryMapper.findSonList(goodCategoryInfo1);
            return sonList;
        }
    }
    /**
        查询所有一级目录
     * @params:
     * @return
     */
    @Override
    public PageInfo<GoodCategoryInfo1> findAllParent(int pageNum,int pageSize) {
        return PageHelper.startPage(pageNum,pageSize).
                doSelectPageInfo(()->goodCategoryMapper.findAllParent());
    }
    /**
        根绝一级分类的ID查询所有的二级分类
     * @params:
     * @return
     */
    @Override
    public PageInfo<GoodCategoryInfo2> findSortById(PageRequest pageRequest, GoodCategoryInfo1 goodCategoryInfo1) {
        return PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getLimit()).
                doSelectPageInfo(()->goodCategoryMapper.findSortById(goodCategoryInfo1));
    }


    /**
        删除目录
     * @params:
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void deleteSortById(GoodCategoryInfo1 goodCategoryInfo1) {
        //判断ID值是一级类目还是二级子目
        if(!goodCategoryInfo1.getUpperName().isEmpty()){
            //说明为1级目录的ID值
            goodCategoryMapper.deleteSortById1(goodCategoryInfo1);
        }else {
            //说明为2级子目的ID值
            goodCategoryMapper.deleteSortById2(goodCategoryInfo1);
        }
    }
    /**
        根据ID查找目录分类，并显示出来修改使用
     * @params:
     * @return
     */
    @Override
    public List<Map> getSort(GoodCategoryInfo1 goodCategoryInfo1) {
        //判断ID属于哪个分类表
        if(!goodCategoryInfo1.getUpperName().isEmpty()){
            //说明为1级目录的ID值
            return goodCategoryMapper.getSort1(goodCategoryInfo1);
        }else {
            //说明为2级子目的ID值
            return goodCategoryMapper.getSort2(goodCategoryInfo1);
        }
    }
    /**
        修改分类
     * @params:
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void updateSort(GoodCategoryInfo1 goodCategoryInfo1) {
        //判断上一级是哪一级
        if(goodCategoryInfo1.getUpperName().equals("所有分类")){
            //说明需要修改的是一级目录
            goodCategoryMapper.updateSort1(goodCategoryInfo1);
        }else {
            //修改的是子目录
            goodCategoryMapper.updateSort2(goodCategoryInfo1);
        }
    }
    /**
        根据子目录名字查询子目录ID
     * @params:
     * @return
     */
    @Override
    public GoodCategoryInfo2 findSonIdByName(String name) {
        return goodCategoryMapper.findSonIdByName(name);
    }
}
